/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lt.ntec.danblog.server.blog.model.objectify;

import com.google.inject.Inject;
import com.google.inject.Injector;
import java.util.List;
import java.util.Set;
import lt.ntec.danblog.server.blog.model.Post;
import lt.ntec.danblog.server.blog.model.PostDAO;
import lt.ntec.danblog.server.core.GuiceFactory;
import lt.ntec.danblog.server.core.model.*;

/**
 *
 * @author Tadas
 */
class ProxyFactory {
    private static Injector INJECTOR;

    @Inject
    private UserDAO userDAO;
    @Inject
    private TagDAO tagDAO;
    @Inject
    private CommentDAO commentDAO;
    @Inject
    private PostDAO postDAO;
    
    private static ProxyFactory PROXY_FACTORY;
    
    private ProxyFactory() {
        
    }
    
    /**
     * This Singleton approach is needed because PostLoad action cannon access
     * dependency injection
     * 
     * @return 
     */
    public static ProxyFactory getInstance() {
        
        if (PROXY_FACTORY != null) {
            return PROXY_FACTORY;
        }
        
        ProxyFactory proxyFactory =
                new ProxyFactory();
        INJECTOR = GuiceFactory.
                getInjector();
        
        INJECTOR.injectMembers(proxyFactory);
        
        PROXY_FACTORY = proxyFactory;
        
        return PROXY_FACTORY;
    }
    
    public static void setInjector(Injector injector) {
        ProxyFactory.INJECTOR = injector;
    }

    public User getPostUserProxy(BlogPost blogPost) {
        return PostUserProxy.newInstance(blogPost, userDAO);
    }

    public Set<? extends Tag> getTagListProxy(BlogPost blogPost) {
        return TagListProxy.newInstance(blogPost, tagDAO);
    }

    public Post getPostProxy(BlogComment blogComment) {
        return PostParentProxy.newInstance(blogComment, postDAO);
    }

    public List<Comment> getCommentList(BlogPost post) {
        return CommentListProxy.newInstance(post, commentDAO);
    }

    public User getCommentUserProxy(BlogComment comment) {
        return CommentUserProxy.newInstance(comment, userDAO);
    }
}
